package com.bulletelc.apollopre.module.display.product.details.introduce;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.entity.ProductEav;
import com.bulletelc.apollopre.entity.ProductEavValueBean;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * author: lhl
 * date: 2018/1/16
 * desc: 商品选项卡的adapter
 */
public class ChooseAdapter extends BaseAdapter {
    private Context context;
    private List<ProductEav> productEavs;
    private LayoutInflater inflater;
    private OnItemClickListener onItemClickListener;

    public ChooseAdapter(Context context, List<ProductEav> productEavs) {
        this.context = context;
        this.productEavs = productEavs;
        inflater = LayoutInflater.from(context);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override

    public int getCount() {
        return productEavs != null ? productEavs.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return productEavs.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = inflater.inflate(R.layout.product_eav_gvlist, viewGroup, false);
            holder = new ViewHolder();
            holder.tvAttributeName = view.findViewById(R.id.tvAttributeName);
            holder.gvValue = view.findViewById(R.id.gvValue);
            view.setTag(holder);
            AutoUtils.autoSize(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tvAttributeName.setText(productEavs.get(i).getAttribute_name());

//        String value = productEavs.get(i).getValue();
//        Map<String, String> maps = (Map) JSON.parse(value);
//        List<ProductEavValueBean> eavValueBeans = new ArrayList<>();
//        for (Map.Entry<String, String> entry : maps.entrySet()) {
//            ProductEavValueBean eavValueBean = new ProductEavValueBean();
//            eavValueBean.setId(entry.getKey());
//            eavValueBean.setName(entry.getValue());
//            eavValueBeans.add(eavValueBean);
//        }
        ValueAdapter adapter = new ValueAdapter(context, productEavs.get(i).getProductEavValueBeans(), i);
        adapter.setOnItemClickListener(new OnValueItemClickListener() {
            @Override
            public void onItemClick(int groupPosition, int childPosition, boolean isChecked) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(groupPosition, childPosition, isChecked);
                }

            }
        });
        holder.gvValue.setAdapter(adapter);

        return view;
    }


    class ViewHolder {
        TextView tvAttributeName;
        GridView gvValue;
    }

    private class ValueAdapter extends BaseAdapter implements View.OnClickListener {
        private Context context;
        private List<ProductEavValueBean> eavValueBeans;
        private int groupPosition;
        private LayoutInflater inflater;

        private OnValueItemClickListener onValueItemClickListener;

        public void setOnItemClickListener(OnValueItemClickListener onValueItemClickListener) {
            this.onValueItemClickListener = onValueItemClickListener;
        }

        public ValueAdapter(Context context, List<ProductEavValueBean> eavValueBeans, int groupPosition) {
            this.context = context;
            this.eavValueBeans = eavValueBeans;
            this.groupPosition = groupPosition;
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return eavValueBeans.size();
        }

        @Override
        public Object getItem(int position) {
            return eavValueBeans.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ValueViewHolder valueHolder;
            if (convertView == null) {
                valueHolder = new ValueViewHolder();
                convertView = inflater.inflate(R.layout.product_eav_value_item, parent, false);
                valueHolder.cbValue = convertView.findViewById(R.id.cbEavValue);
                valueHolder.cbValue.setOnClickListener(this);
                convertView.setTag(R.id.tag_product_eav_value_holder, valueHolder);
                AutoUtils.autoSize(convertView);
            } else {
                valueHolder = (ValueViewHolder) convertView.getTag(R.id.tag_product_eav_value_holder);
            }
            valueHolder.cbValue.setText(eavValueBeans.get(position).getName());
            valueHolder.cbValue.setChecked(eavValueBeans.get(position).isChecked());
            valueHolder.cbValue.setEnabled(!eavValueBeans.get(position).isChecked());
            valueHolder.cbValue.setTag(position);
            return convertView;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.cbEavValue:
                    int position = (int) v.getTag();
                    boolean isChecked = ((CheckBox) v).isChecked();
                    if (onValueItemClickListener != null) {
                        onValueItemClickListener.onItemClick(groupPosition, position, isChecked);
                    }
                    break;
            }
        }

        class ValueViewHolder {
            CheckBox cbValue;
        }


    }

    interface OnValueItemClickListener {
        void onItemClick(int groupPosition, int childPosition, boolean isChecked);
    }

    interface OnItemClickListener {
        void onItemClick(int groupPosition, int childPosition, boolean isChecked);
    }
}
